package cn.ds.assets.module.assets.service.metadataJobCollect;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.time.LocalDateTime;

import cn.ds.assets.module.assets.controller.admin.metadataJobCollect.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.metadataJobCollect.BaseMetadataJobCollectDO;
import cn.ds.assets.framework.common.pojo.PageResult;

import cn.ds.assets.module.assets.convert.metadataJobCollect.BaseMetadataJobCollectConvert;
import cn.ds.assets.module.assets.dal.mysql.metadataJobCollect.BaseMetadataJobCollectMapper;

import static cn.ds.assets.framework.common.exception.util.ServiceExceptionUtil.exception;
import cn.ds.assets.framework.common.exception.enums.GlobalErrorCodeConstants;
//import static cn.ds.assets.module.assets.enums.ErrorCodeConstants.*;

/**
 * 元数据管理-元数据采集作业 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class BaseMetadataJobCollectServiceImpl implements BaseMetadataJobCollectService {

    @Resource
    private BaseMetadataJobCollectMapper baseMetadataJobCollectMapper;

    @Override
    public Long createBaseMetadataJobCollect(BaseMetadataJobCollectCreateReqVO createReqVO) {
        // 插入
        BaseMetadataJobCollectDO baseMetadataJobCollect = BaseMetadataJobCollectConvert.INSTANCE.convert(createReqVO);
        baseMetadataJobCollectMapper.insert(baseMetadataJobCollect);
        // 返回
        return baseMetadataJobCollect.getId();
    }

    @Override
    public void updateBaseMetadataJobCollect(BaseMetadataJobCollectUpdateReqVO updateReqVO) {
        // 校验存在
        validateBaseMetadataJobCollectExists(updateReqVO.getId());
        // 更新
        BaseMetadataJobCollectDO updateObj = BaseMetadataJobCollectConvert.INSTANCE.convert(updateReqVO);
        baseMetadataJobCollectMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaseMetadataJobCollect(Long id) {
        // 校验是否存在
        validateBaseMetadataJobCollectExists(id);
        // 逻辑删除
        baseMetadataJobCollectMapper.updateToDelete(id, LocalDateTime.now());
    }

    private void validateBaseMetadataJobCollectExists(Long id) {
        if (id == null) {
            throw exception(GlobalErrorCodeConstants.ID_NOT_FOUND);
        }
        BaseMetadataJobCollectDO dataDo = baseMetadataJobCollectMapper.selectById(id);
        if (dataDo == null) {
            throw exception(GlobalErrorCodeConstants.DATA_NOT_FOUND);
        }
    }

    @Override
    public BaseMetadataJobCollectDO getBaseMetadataJobCollect(Long id) {
        return baseMetadataJobCollectMapper.selectById(id);
    }

    @Override
    public List<BaseMetadataJobCollectDO> getBaseMetadataJobCollectList(Collection<Long> ids) {
        return baseMetadataJobCollectMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BaseMetadataJobCollectDO> getBaseMetadataJobCollectPage(BaseMetadataJobCollectPageReqVO pageReqVO) {
        return baseMetadataJobCollectMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BaseMetadataJobCollectDO> getBaseMetadataJobCollectList(BaseMetadataJobCollectExportReqVO exportReqVO) {
        return baseMetadataJobCollectMapper.selectList(exportReqVO);
    }

}
